package Experiment;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

public class dealServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String user = req.getParameter("username");
        String passwd = req.getParameter("passWd");
        if (user.equals(passwd)) {
            String keep = req.getParameter("keep");
            if (keep != null) {//如果复选框被选中，即创建两周免登录
                Cookie name = new Cookie("name", "user");
                Cookie mima = new Cookie("passwd", "passWd");
                name.setMaxAge(14 * 24 * 3600);
                mima.setMaxAge(14 * 24 * 3600);
                resp.addCookie(name);
                resp.addCookie(mima);
            }
            //为了用户名和密码的安全性，以Session的方式传递这两个值
            HttpSession hs = req.getSession(true);
            hs.setMaxInactiveInterval(30);//设置session失效时间
            hs.setAttribute("name", user);
            hs.setAttribute("passwd", passwd);
            resp.sendRedirect("cookies");
        }
    }
}
